#include "GameBoard.h"

GameBoard::GameBoard(QWidget *parent)
    : QWidget(parent), gameController(new GameController(this))
{
    setupUI();
    setFocusPolicy(Qt::StrongFocus);
    connect(gameController, &GameController::boardChanged, this, &GameBoard::updateBoard);
    connect(gameController, &GameController::gameOver, this, &GameBoard::showGameOver);
    connect(gameController, &GameController::scoreUpdated, this, &GameBoard::updateScore);
    
    gameController->startNewGame();
}

GameBoard::~GameBoard()
{
}

void GameBoard::setupUI()
{
    setWindowTitle("2048 Game");
    setFixedSize(500, 600);
    
    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    
    // 标题
    QLabel *titleLabel = new QLabel("2048");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setStyleSheet("font-size: 40px; font-weight: bold; color: #776e65; margin: 10px;");
    mainLayout->addWidget(titleLabel);
    
    // 分数面板
    QHBoxLayout *scoreLayout = new QHBoxLayout();
    
    scoreLabel = new QLabel("Score: 0");
    scoreLabel->setStyleSheet("background: #bbada0; color: white; padding: 10px; border-radius: 5px; font-weight: bold;");
    scoreLabel->setAlignment(Qt::AlignCenter);
    
    bestScoreLabel = new QLabel("Best: 0");
    bestScoreLabel->setStyleSheet("background: #bbada0; color: white; padding: 10px; border-radius: 5px; font-weight: bold;");
    bestScoreLabel->setAlignment(Qt::AlignCenter);
    
    restartButton = new QPushButton("New Game");
    restartButton->setStyleSheet("background: #8f7a66; color: white; padding: 10px; border-radius: 5px; font-weight: bold;");
    connect(restartButton, &QPushButton::clicked, this, &GameBoard::restartGame);
    
    scoreLayout->addWidget(scoreLabel);
    scoreLayout->addWidget(bestScoreLabel);
    scoreLayout->addWidget(restartButton);
    mainLayout->addLayout(scoreLayout);
    
    // 游戏网格
    QWidget *gridWidget = new QWidget();
    gridWidget->setStyleSheet("background: #bbada0; padding: 10px; border-radius: 5px;");
    gridLayout = new QGridLayout(gridWidget);
    gridLayout->setSpacing(10);
    gridLayout->setContentsMargins(10, 10, 10, 10);
    
    setupGameGrid();
    mainLayout->addWidget(gridWidget);
    
    // 游戏说明
    QLabel *instructionLabel = new QLabel("Use arrow keys to move tiles. Combine tiles with the same number to reach 2048!");
    instructionLabel->setAlignment(Qt::AlignCenter);
    instructionLabel->setStyleSheet("color: #776e65; margin: 10px;");
    instructionLabel->setWordWrap(true);
    mainLayout->addWidget(instructionLabel);
}

void GameBoard::setupGameGrid()
{
    tileLabels.resize(4);
    for (int i = 0; i < 4; ++i) {
        tileLabels[i].resize(4);
        for (int j = 0; j < 4; ++j) {
            QLabel *tile = createTileLabel(0);
            gridLayout->addWidget(tile, i, j);
            tileLabels[i][j] = tile;
        }
    }
}

QLabel* GameBoard::createTileLabel(int value)
{
    QLabel *label = new QLabel();
    label->setAlignment(Qt::AlignCenter);
    label->setFixedSize(100, 100);
    
    updateTileLabel(label, value);
    
    return label;
}

void GameBoard::updateTileLabel(QLabel *label, int value)
{
    QString text = value == 0 ? "" : QString::number(value);
    QString color = getTileColor(value);
    QString textColor = getTileTextColor(value);
    
    QString style = QString(
        "background: %1;"
        "color: %2;"
        "font-size: %3px;"
        "font-weight: bold;"
        "border-radius: 5px;"
    ).arg(color).arg(textColor).arg(value < 100 ? 35 : value < 1000 ? 30 : 24);
    
    label->setText(text);
    label->setStyleSheet(style);
}

QString GameBoard::getTileColor(int value)
{
    switch (value) {
    case 0: return "#cdc1b4";
    case 2: return "#eee4da";
    case 4: return "#ede0c8";
    case 8: return "#f2b179";
    case 16: return "#f59563";
    case 32: return "#f67c5f";
    case 64: return "#f65e3b";
    case 128: return "#edcf72";
    case 256: return "#edcc61";
    case 512: return "#edc850";
    case 1024: return "#edc53f";
    case 2048: return "#edc22e";
    default: return "#3c3a32";
    }
}

QString GameBoard::getTileTextColor(int value)
{
    return value < 8 ? "#776e65" : "#f9f6f2";
}

void GameBoard::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Up:
        gameController->move(GameController::UP);
        break;
    case Qt::Key_Down:
        gameController->move(GameController::DOWN);
        break;
    case Qt::Key_Left:
        gameController->move(GameController::LEFT);
        break;
    case Qt::Key_Right:
        gameController->move(GameController::RIGHT);
        break;
    default:
        QWidget::keyPressEvent(event);
        return;
    }
}

void GameBoard::updateBoard()
{
    auto board = gameController->getBoard();
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            updateTileLabel(tileLabels[i][j], board[i][j]);
        }
    }
}

void GameBoard::showGameOver()
{
    QMessageBox::information(this, "Game Over", "Game Over! Click New Game to play again.");
}

void GameBoard::restartGame()
{
    gameController->startNewGame();
}

void GameBoard::updateScore(int score)
{
    scoreLabel->setText(QString("Score: %1").arg(score));
}
